<?php
namespace App\Controller;
use Think\Controller;
class DiaryController extends Controller {
    public function createDiary($userId, $title, $createTime){
        $diary = M('_diary');
        $data['userid'] = $userId;
        $data['title'] = $title;
        $data['createtime'] = $createTime;
        $res['id'] = $diary->add($data);
        if ($res['id'] != null) {
            $res['code'] = 4;
            $res['msg'] = "创建日志成功";
            $this->ajaxReturn($res,'JSON');
        }
    }

    public function fillDiary($diaryId, $type, $info, $sequence){
        $diaryContent = M('_diary_content');
        $data['diaryid'] = $diaryId;
        $data['type'] = $type;
        $data['info'] = $info;
        $data['sequence'] = $sequence;

        // 存在就替换
        $map['diaryid'] = $diaryId;
        $map['sequence'] = $sequence;
        $uniqe = $diaryContent->where($map)->find();
        if ($uniqe != null) {
            $res = $diaryContent->where($map)->save($data);
            $response['code'] = '4';
            $response['msg'] = '更新成功';
        
            $this->ajaxReturn($response,'JSON');
        } else{
            $res = $diaryContent->add($data);
            $response['code'] = '4';
            $response['msg'] = '添加成功';
            
            $this->ajaxReturn($response,'JSON');
        }
    }

    public function getDiaries($userId){
         $diary = M('_diary');
         $map["userid"] = $userId;

         $res['diaries'] = $diary->where($map)->order('createtime desc')->select();
         if ($res['diaries'] != null) {
             $res['code'] = 2;
             $res['msg'] = '获取日记数据';
             $this->ajaxReturn($res, 'JSON');
         }
    }

    public function getDiaryContent($diaryId){
        $diary = M('_diary_content');
        $map['diaryid'] = $diaryId;
        $res['contents'] = $diary->where($map)->order('sequence asc')->select();
        if ($res['contents'] != null) {
            $res['code'] = 2;
            $res['msg'] = "获取日记内容";
            $this->ajaxReturn($res, 'JSON');
        }
    }

    public function updateDiary($diaryId){
        $diary = M('_diary');
        $map["diaryid"] = $diaryId;
        $data['title'] = I('post.title');
        $r = $diary->where($map)->save($data);
        $res['code'] = 4;
        $res['msg'] = '更新成功';
        $this->ajaxReturn($res,'JSON');
    }

    //最新日志
    public function getLatestBrife($userId){
        $diary = M('_diary');
        $map['userid'] = $userId;
        $d = $diary->where($map)->order('createtime desc')->find();
        $diaryContent = M('_diary_content');
        if ($d!=null) {
            $map_txt['diaryid'] = $d['id'];
            $map_txt['type'] = 'diary_text_content';
            $map_img['diaryid'] = $d['id'];
            $map_img['type'] = 'diary_img_content';
            $txt = $diaryContent->where($map_txt)->order('sequence')->find();
            $img = $diaryContent->where($map_img)->order('sequence')->find();
            // 返回数据
            $res['diary'] = $d;
            $res['diaryContents'] = array($txt, $img);
            $res['code'] = 2;
            $res['msg'] = '获取日记简介内容';
            $this->ajaxReturn($res, 'JSON');
        }else{
            $res['diary'] = null;
            $res['diaryContents'] = null;
            $res['code'] = 4;
            $res['msg'] = '无日记';
            $this->ajaxReturn($res, 'JSON');
        }
    }
}